﻿#include "Wnd.h"
#include <atlimage.h>

int DifficultyChoose = 1;
int _Color[3] = { 0,0,0 };
int _Color2[3] = { 0,0,0 };
int _PlayerBaseHP,_AIBaseHP;
int _MousePosX=0, _MousePosY=0;

BEGIN_MESSAGE_MAP(Wnd, CFrameWnd)
	ON_WM_LBUTTONDOWN()
	ON_WM_PAINT()
	ON_WM_KEYDOWN()
	ON_WM_TIMER()
	ON_WM_ERASEBKGND()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONUP()
END_MESSAGE_MAP()

void Wnd::OnLButtonDown(UINT nFlags, CPoint point)
{
	click = true;
	if (TRUE == nFlags)
	{
		_MousePosX = point.x;
		_MousePosY = point.y;
	}
}

void Wnd::OnPaint()
{
	CPaintDC dc(this);
	if (GM->GetGameState() == GameManager::GameState::START)
	{
		if ((_MousePosX >= 535 && _MousePosX <= 658) && (_MousePosY >= 105 && _MousePosY <= 168))
		{
			GM->StartGame();
			objects.insert(GM->GetPlayerController()->GetBase());
			objects.insert(GM->GetAIController()->GetBase());
		}
		else if (((_MousePosX >= 449 && _MousePosX <= 511) && (_MousePosY >= 308 && _MousePosY <= 330)))
		{
			GM->SetGameDifficulty(AIController::GameDifficulty::EASY);
			DifficultyChoose = 1;
		}
		else if (((_MousePosX >= 430 && _MousePosX <= 525) && (_MousePosY >= 358 && _MousePosY <= 381)))
		{
			GM->SetGameDifficulty(AIController::GameDifficulty::NORMAL);
			DifficultyChoose = 2;
		}
		else if (((_MousePosX >= 449 && _MousePosX <= 511) && (_MousePosY >= 413 && _MousePosY <= 434)))
		{
			GM->SetGameDifficulty(AIController::GameDifficulty::HARD);
			DifficultyChoose = 3;
		}
		bgChooseProcess->update(mdcBuffer);
		CRect rectX;
		GetClientRect(&rectX);
		_Color2[0] = 255;
		if (DifficultyChoose == 1)
		{
			PaintTheText(mdcBuffer, rectX, "Easy", _Color2, bg->x, 50);
			PaintTheText(mdcBuffer, rectX, "Medium", _Color, bg->x, 100);
			PaintTheText(mdcBuffer, rectX, "Hard", _Color, bg->x, 155);
		}
		else if (DifficultyChoose == 2)
		{
			PaintTheText(mdcBuffer, rectX, "Easy", _Color, bg->x, 50);
			PaintTheText(mdcBuffer, rectX, "Medium", _Color2, bg->x, 100);
			PaintTheText(mdcBuffer, rectX, "Hard", _Color, bg->x, 155);
		}
		else if (DifficultyChoose == 3)
		{
			PaintTheText(mdcBuffer, rectX, "Easy", _Color, bg->x, 50);
			PaintTheText(mdcBuffer, rectX, "Medium", _Color, bg->x, 100);
			PaintTheText(mdcBuffer, rectX, "Hard", _Color2, bg->x, 155);
		}
		PaintTheText(mdcBuffer, rectX, "Play", _Color, 118+bg->x, -135);
	}
	else if (GM->GetGameState() == GameManager::GameState::GAME)
	{
		bg->update(mdcBuffer);
		GM->Update(&objects);
	
		_PlayerBaseHP = GM->GetPlayerController()->GetBase()->GetHp();
		_AIBaseHP = GM->GetAIController()->GetBase()->GetHp();
		_Color[0] = 255;
		_Color[1] = 0;
		_Color[2] = 0;
		CRect rectX;
		GetClientRect(&rectX);
		PaintCost(mdcBuffer,rectX,"HP:","宋体",200,_PlayerBaseHP,_Color,-400,-165);
		PaintCost(mdcBuffer, rectX, "HP:", "宋体", 200, _AIBaseHP,_Color, 625, -165);
	}
	else if (GM->GetGameState() == GameManager::GameState::LOSE || GM->GetGameState() == GameManager::GameState::WIN)
	{
		_Color[0] = 0;
		_Color[1] = 0;
		_Color[2] = 0;
		bgEnd->update(mdcBuffer);
		CRect rectX;
		GetClientRect(&rectX);
		if (GM->GetGameState() == GameManager::GameState::LOSE)
		{
			PaintTheText(mdcBuffer,rectX,"You have lost the war!",_Color2,bg->x,-100);
		}
		else if (GM->GetGameState() == GameManager::GameState::WIN)
		{
			PaintTheText(mdcBuffer, rectX, "You win the war!", _Color2, bg->x, -100);
		}
		PaintTheText(mdcBuffer, rectX, "Restart the Game", _Color, bg->x, 0);
		PaintTheText(mdcBuffer, rectX, "Exit", _Color, bg->x, 75);
		if ((_MousePosX >= 346 && _MousePosX <= 614) && (_MousePosY >= 257 && _MousePosY <= 280))
		{
			GM->RestartGame();
			objects.clear();
			objects.insert(gmenu);
			gmenu->setPercent(0);
			gmenu->reCount();
			bgChooseProcess->getMove(bg->x);
			skill->reload();
		}
		else if ((_MousePosX >= 447 && _MousePosX <= 511) && (_MousePosY >= 332 && _MousePosY <= 355))
		{
			PostQuitMessage(0);
		}
	}

	std::set<GameObject*>::iterator iter;
	for (iter = objects.begin(); iter != objects.end(); ++iter)
	{
		GameObject* obj = *iter;
		if (GM->GetGameState() == GameManager::GameState::GAME) {
			if (obj->kind == 1 && obj->iserase) {
				iter=objects.erase(iter);
				if (iter == objects.end()) break;
			}
			else {
				obj->update(mdcBuffer);
			}
			skill->updatestate();
		}
	}
	if(!click)
	{
		mouse.AlphaBlend(mdcBuffer->m_hDC, mousex + gmenu->x, mousey, mouse.GetWidth(), mouse.GetHeight(), 0, 0, mouse.GetWidth(), mouse.GetHeight());
	}
	else {
		mousedown.AlphaBlend(mdcBuffer->m_hDC, mousex + gmenu->x, mousey, mousedown.GetWidth(), mousedown.GetHeight(), 0, 0, mousedown.GetWidth(), mousedown.GetHeight());
	}
	if (GM->GetGameState() == GameManager::GameState::GAME) {

		if (GM->GetPlayerController()->GetTrainTime() != 0) {
			gmenu->setPercent((double)(GM->GetPlayerController()->GetLastTime()) / (double)(GM->GetPlayerController()->GetTrainTime()));
		}
		CRect rectX;
		GetClientRect(&rectX);
		gmenu->PaintNumber(mdcBuffer, rectX, "Gold:", "XP:","","","", GM->GetPlayerController()->GetGold(), GM->GetPlayerController()->GetXp(),GM->GetPlayerController()->GetSoldierData(1)->price,GM->GetPlayerController()->GetSoldierData(0)->price,GM->GetPlayerController()->GetCurAgeXpCost(), skill->getremaintime(),bg->x);
	}

	dc.BitBlt(0, 0, 960, 540, mdcBuffer, bg->x, bg->y, SRCCOPY);
}

void Wnd::OnTimer(UINT_PTR nIDEvent)
{
	display();
	CFrameWnd::OnTimer(nIDEvent);
}

void Wnd::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	if (VK_ESCAPE == nChar) {
		PostQuitMessage(0);
	}

	CFrameWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}

BOOL Wnd::OnEraseBkgnd(CDC* pDC)
{
	//return CFrameWnd::OnEraseBkgnd(pDC);
	return TRUE;
}

void Wnd::display()
{
	CRect rect;
	GetClientRect(&rect);
	InvalidateRect(rect);
}

void Wnd::OnMouseMove(UINT nFlags, CPoint point)
{
	mousex = point.x, mousey = point.y;
	if (GM->GetGameState() == GameManager::GameState::GAME)
	{
		if (point.x > 860&&point.y<315) {
			bg->getDirection(1);
			gmenu->getDirection(1);
		}
		else if (point.x < 100 && point.y < 315) {
			bg->getDirection(2);
			gmenu->getDirection(2);
		}
		else{
			bg->getDirection(0);
			gmenu->getDirection(0);
		}
	}
	CFrameWnd::OnMouseMove(nFlags, point);
}

void Wnd::OnLButtonUp(UINT nFlags, CPoint point)
{
	click = false;
	if (GM->GetGameState() == GameManager::GameState::GAME) {
		//升级时代
		if (point.x > gmenu->getbt_x(4) && point.x < gmenu->getbt_x(4) + 60 && point.y>gmenu->getbt_y(4) && point.y < gmenu->getbt_y(4) + 60)
		{
			if (GM->GetPlayerController()->UpdateAge()) {
				gmenu->timeUp();
			}
		}
		//士兵1
		if (point.x > gmenu->getbt_x(1) && point.x < gmenu->getbt_x(1) + 85 && point.y>gmenu->getbt_y(1) && point.y < gmenu->getbt_y(1) + 85)
		{
			GM->GetPlayerController()->TrainASoldier(0);
			gmenu->setCount();
		}
		//士兵2
		if (point.x > gmenu->getbt_x(2) && point.x < gmenu->getbt_x(2) + 85 && point.y>gmenu->getbt_y(2) && point.y < gmenu->getbt_y(2) + 85)
		{
			GM->GetPlayerController()->TrainASoldier(1);
			gmenu->setCount();
		}
		//技能  技能中的iserase表示是否将石头移出set中，由于是同时的一批石头，所以也就是用这个判断造成伤害
		if (point.x > gmenu->getbt_x(3) && point.x < gmenu->getbt_x(3) + 143 && point.y>gmenu->getbt_y(3) && point.y < gmenu->getbt_y(3) + 123)
		{
			if (skill->isready) {
				sound->PlayAbilitySound();
				skill->set(s, &objects);
				for (std::set<GameObject*>::iterator iter = objects.begin(); iter != objects.end(); ++iter) {
					if (typeid(**iter).name() == typeid(AISoldier).name()) {
						AISoldier* ais = (AISoldier*)*iter;
						ais->UnderAttack(ais->GetMaxHp());
					}
				}
			}
		}
	}

	CFrameWnd::OnLButtonUp(nFlags, point);
}
